package com.cs.game.core.utils;

import com.cs.game.core.constant.Constants;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Score file tools
 */
public class ScoreFileUtil {
    /**
     * Read text from filename
     * @param filename filename
     * @return Text collection
     */
    public static List<String> readFileByLine(String filename) {
        File file = new File(filename);
        List<String> list = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line = null;
            while ((line = reader.readLine()) != null) {
                list.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * Write string to file
     * @param str
     */
    public static void writeLineToFile(String str) {
        File file = new File(ScoreFileUtil.class.getClassLoader().getResource(Constants.FILE_SCOER_PATH).getPath());
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
            bw.write(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
